<?php
    class Pager
    {
        /**
         * Maximum items per page
         *
         * @var int
         */
        private $maxPerPage;

        /**
         * Current page number
         *
         * @var int
         */
        private $page;

        /**
         * Number of results found
         *
         * @var int
         */
        private $numResults;

        /**
         * An array of results found
         *
         * @var array
         */
        private $results;


        public function __construct( $page, $maxPerPage )
        {
            $this->page = $page;
            $this->maxPerPage = $maxPerPage;

            $this->results = array();
            $this->numResults = 0;
        }

        public function GetPage()
        {
            return $this->page;
        }

        public function GetMaxPerPage()
        {
            return $this->maxPerPage;
        }

        public function SetResults( $results, $total )
        {
            $this->results = (array)$results;
            $this->numResults = $total;
        }

        public function GetResults()
        {
            return $this->results;
        }

        public function GetNumResults()
        {
            return $this->numResults;
        }

        public function HaveToPaginate()
        {
            return $this->GetNumResults() > $this->GetMaxPerPage();
        }

        public function GetNextPage()
        {
            return min( $this->GetPage() + 1, $this->GetLastPage() );
        }

        public function GetPreviousPage()
        {
            return max( $this->GetPage() - 1, $this->GetFirstPage() );
        }

        public function GetFirstPage()
        {
            return 1;
        }

        public function GetLastPage()
        {
            return max( 1, ceil( $this->GetNumResults() / $this->GetMaxPerPage() ) );
        }

        public function GetPages( $numPages )
        {
            $pages = array();
            $tmp   = $this->page - floor($numPages / 2);
            $check = $this->GetLastPage() - $numPages + 1;
            $limit = ($check > 0) ? $check : 1;
            $begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;

            $i = (int) $begin;
            while (($i < $begin + $numPages) && ($i <= $this->GetLastPage()))
            {
                $pages[] = $i++;
            }

            return $pages;
        }

        public function GetFirstRecNum()
        {
            if ($this->page == 0)
            {
                return 1;
            }
            else
            {
                return ($this->page - 1) * $this->maxPerPage + 1;
            }
        }

        public function GetLastRecNum()
        {
            if ($this->page == 0)
            {
                return $this->numResults;
            }
            else
            {
                if ( ($this->page * $this->maxPerPage) >= $this->numResults )
                {
                    return $this->numResults;
                }
                else
                {
                    return ($this->page * $this->maxPerPage);
                }
            }
        }
    }
?>
